﻿using Microsoft.Win32;
using System.Diagnostics;
using System.Globalization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using PPJoy_dotNET;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.Reflection;
using System.ServiceProcess;

namespace DroidPad
{
    public static class DPsf
    {
        public static NumberFormatInfo englishLocale = new NumberFormatInfo();

        /// <summary>
        /// Checks whether a program is installed (32 & 64-bit)
        /// </summary>
        /// <param name="itemName">Program's regristy name</param>
        /// <returns></returns>
        public static bool checkInstall(string itemName)
        {
            // Checks 32-bit and 64-bit.

            RegistryKey regK = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + itemName);//PPJoy Joystick Driver");
            if (regK != null)
                return true;
            regK = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + itemName);//PPJoy Joystick Driver");
            if (regK != null)
                return true;
            return false;
        }
        /// <summary>
        /// Checks for PPJoy
        /// </summary>
        /// <returns></returns>
        public static bool checkPPJoy()
        {
            return checkInstall("PPJoy Joystick Driver");
        }
        /// <summary>
        /// Checks for the old version of PPJoy
        /// </summary>
        /// <returns></returns>
        public static bool checkOldPPJoy()
        {
            return checkInstall("Parallel Port Joystick");
        }
        public static string getPPJoyUninst()
        {
            RegistryKey regK = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\PPJoy Joystick Driver");//PPJoy Joystick Driver");
            if (regK != null)
                return (string)regK.GetValue("UninstallString");
            // 64-bit
            regK = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\PPJoy Joystick Driver");//PPJoy Joystick Driver");
            if (regK != null)
                return (string)regK.GetValue("UninstallString");
            return null;
        }
        /// <summary>
        /// Get the location of PPJoy's control panel app.
        /// </summary>
        /// <returns></returns>
        public static string getPPJoyCPL()
        {
            string dir = getPPJoyUninst();
            dir = dir.Substring(0, dir.LastIndexOf(@"\"));
            //MessageBox.Show(dir);
            return "\"" + dir + "\\PPortJoy.cpl\"";
        }
        /// <summary>
        /// Checks whether the Nexus One / G1 (htc) drivers are installed.
        /// </summary>
        /// <returns></returns>
        public static bool checkUSBInstall()
        {
            System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(new System.Management.SelectQuery("Win32_SystemDriver"));
            foreach (System.Management.ManagementObject ManageObject in searcher.Get())
            {
                if (ManageObject["Description"].ToString() == "Android USB Driver")
                {
                    return true;
                }
                //textBox1.Text += "\r\n" + ManageObject["Name"].ToString() + "\t\t" + ManageObject["Description"].ToString();
            }
            return false;
        }
        /// <summary>
        /// Enables or disables "test signing" in win64 (Vista & 7)
        /// </summary>
        /// <param name="onOff">If true, will enable test signing. Otherwise, will disable it.</param>
        public static void testSign(bool onOff)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "bcdedit.exe";
                p.StartInfo.Arguments = "-set TESTSIGNING " + (onOff ? "ON" : "OFF");
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.UseShellExecute = false;
                p.Start();
                p.WaitForExit();
            }
            catch { }
        }
        /// <summary>
        /// Restarts the computer
        /// </summary>
        public static void restart()
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "shutdown.exe";
                p.StartInfo.Arguments = "-r";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.UseShellExecute = false;
                p.Start();
            }
            catch { }
        }

        public static float TrimMinMax(float num, float min, float max)
        {
            if (num < min)
                return min;
            if (num > max)
                return min;
            return num;
        }

        public static void SendStatMessage(string message)
        {
            StreamWriter log;
            if (!File.Exists(Properties.Settings.Default.logPath))
            {
                log = new StreamWriter(Properties.Settings.Default.logPath);
            }
            else
            {
                log = File.AppendText(Properties.Settings.Default.logPath);
            }
            log.WriteLine(message);
            log.Close();
        }

        public static void SendErrorMessage(string message)
        {
            SendStatMessage(message);
        }
    }
}